﻿using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// Helper class for PriceFilter
/// </summary>

namespace si.caha.Helper
{
    public class PriceFilter
    {
        public int PriceFrom { get; set; }
        public int PriceTo { get; set; }

        public PriceFilter(int from, int to)
        {
            PriceFrom = from;
            PriceTo = to;
        }

        public string ToUrlString(String currentUrl, int currentPageNumber)
        {
            string result = RemoveFilterAndPagnation(currentUrl, currentPageNumber);

            if (result.Contains("?"))
                return result += "&priceFrom=" + PriceFrom + "&priceTo=" + PriceTo;
            else
                return result += "?priceFrom=" + PriceFrom + "&priceTo=" + PriceTo;
        }

        public static string RemoveFilterAndPagnation(string currentUrl, int currentPageNumber)
        {
            string result = RemoveFilter(currentUrl);
            return Pagnation.RemoveFilter(result, currentPageNumber);
        }

        private static string RemoveFilter(string currentUrl)
        {
            foreach (PriceFilter priceFilter in GetPriceFilters())
            {
                currentUrl = currentUrl.Replace("&priceFrom=" + priceFilter.PriceFrom + "&priceTo=" + priceFilter.PriceTo, "");
                currentUrl = currentUrl.Replace("?priceFrom=" + priceFilter.PriceFrom + "&priceTo=" + priceFilter.PriceTo, "");
            }
            return currentUrl;
        }

        public override string ToString()
        {
            return PriceFrom + " - " + PriceTo;
        }

        public static List<PriceFilter> GetPriceFilters()
        {
            return new List<PriceFilter>() {
                new PriceFilter(0, 50),
                new PriceFilter(50, 100),
                new PriceFilter(100, 500)
            };
        }
    }
}